var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModelDel: false,
    orderList : '',     //初始化订单
    value:'',           //输入框值初始化
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var value = options.value;
    // value = '201809061509852831';
    var token = wx.getStorageSync('token');             //用户的token信息
    that.setData({
      token: token,
      value: value
    })
    console.log(value)
    that.getOrderByOrderNo(token,value)
  },

  /***
   * 搜索订单
  */
  searchInput: function (e) {
    var that = this;
    var value = e.detail.value;
    var token = that.data.token;
    that.setData({
      value: value
    })
    that.getOrderByOrderNo(token, value);
  },

  /**
   * 清空输入框
   */
  clearOrder :function(){
    this.setData({
      value: ''
    })
  },

  /**
   * 查询订单根据订单号
   */
  getOrderByOrderNo: function (token,orderNo){
    var that= this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/getOrdersByState", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        console.log(res)
        if (code == "10000") {
          var orderList = res.data.orderList;
          that.setData({
            orderList: orderList
          });
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 开启是否取消
   */
  showModel: function (e) {
    var that = this;

    var orderNo = e.currentTarget.dataset.orderno;
    that.setData({
      orderNo: orderNo
    })

    that.setData({
      showModel: true
    })
  },


  /**
   * 删除订单
   */
  showModelDel: function (e) {
    var that = this;

    var orderNo = e.currentTarget.dataset.orderno;
    that.setData({
      orderNo: orderNo
    })

    that.setData({
      showModelDel: true
    })
  },

  /**
   * 取消确认按钮
   */
  confirmQx: function () {
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/cancelorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.showMsg(msg, 'success');
          //刷新列表
          var state = that.data.state;
          if (state == 0) {
            that.initMyorders(token, 0, 10);
          } else {
            that.getOrdersByState(token, state, 0, 10);
          }

        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 删除订单按钮
   */
  confirmDel: function () {
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/delorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.showMsg(msg, 'success');
          //刷新列表
          var state = that.data.state;
          if (state == 0) {
            that.initMyorders(token, 0, 10);
          } else {
            that.getOrdersByState(token, state, 0, 10);
          }

        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 关闭选择规格
   */
  closeModel: function () {
    var that = this;
    that.setData({
      showModel: false,
      showModelDel: false,
    })
  },

  /**
   * 跳转到订单详情
   */
  goOrderDetail: function (e) {
    var orderNo = e.currentTarget.dataset.orderno;
    wx.navigateTo({
      url: "../../order/orderDetail/orderDetail?orderNo=" + orderNo,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /***
   * 查看物流
  */
  logistics: function () {
    this.goPage('../../order/logistics/logistics')
  },
  /**
* 跳转页面
*/
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();
      }
    })
  },
  /**
   * 提示信息
   */
  showMsg: function (msg, icon) {
    wx.showToast({
      title: msg,
      icon: icon,
      duration: 2000,
      mask: true
    });
  },
})